package sub

import (
	"fmt"
	"quipus/cmd/qpc/sub"
	"quipus/internal/utils"
	v1pb "quipus/proto/gen/api/v1"

	"github.com/spf13/cobra"
)

var (
	id int32
)

func init() {

	deleteCmd.PersistentFlags().Int32VarP(&id, "id", "i", 0, "model id")
	resourceCmd.AddCommand(deleteCmd)
}

var deleteCmd = &cobra.Command{
	Use:   "delete",
	Short: "Run cmd to delete resource",
	RunE: func(cmd *cobra.Command, args []string) error {
		qconn, e := sub.NewGrpcConnect(sub.QpConfig.QPAddress)
		if e != nil {
			return e
		}

		qclient := v1pb.NewResourceServiceClient(qconn)
		ctxWithToken := sub.NewCtxWithToken(sub.QpConfig.Token)
		urn := utils.BuildUrn(utils.ResourceUrnPrefix, id)

		in := &v1pb.DeleteResourceRequest{Urn: urn}
		rsp, err := qclient.DeleteResource(ctxWithToken, in)
		if err != nil {
			return err
		}
		fmt.Print(rsp)
		return nil
	},
}
